<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja"
      xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<head>
  <title>Ubiquity Web Search Commands for Japanese, nora - yahoo-news</title>
</head>
<body>
<p>This xhtml source contains commands for searching the Web.  Its
corresponding feed is <a href="ubiquity-commands-ja-nora-yahoo-news.html">here</a>.</p>

<div id="yahoo-news" style="display: none;">
<b>${query}</b>で Yahoo! news を検索:
{for item in items}
  <div style="font-size:90%;">
    <div style="border:1px solid #777777; -moz-border-radius:4px;background:#333333;margin:2px 0px 0px 0px;padding:0px;">
        <div style="border:1px solid #777777; -moz-border-radius:4px;background:#666666;margin:0px;padding:0px;">
            <A href="${item.url}" accesskey="${item.key}"><span style="font-size:xx-small; border-width:1px; border-style:solid;-moz-border-radius:2px; padding:0px 2px 0px 2px; vertical-align:center; margin-left:1px; max-width:20px; max-height:10px; overflow:visible;float:none;background: #dddddd;color:#333333;">${item.key}</span><b>${item.title}</b></A>
        </div>
        {if item.photo}
            <div style="float:right;margin:1px 1px 1px 3px;">${item.photo}</div>
        {/if}
        <div style="margin-left: 10px; font-size: 85%;">
            ${item.description}
        </div>
        <div style="margin-left:10px;font-size:70%;">
            ${item.date}${item.typeImg}
        </div>
        <div style="clear:both;"> </div>
    </div>
  </div>
{forelse}
  <b>該当項目が見つかりませんでした</b>
{/for}
    <div style="font-size:70%;clear:both;margin-top:7px;">※[Ctrl]+[Alt]+[番号] でジャンプ</div>
</div>

<script class="commands"><![CDATA[

CmdUtils.CreateCommand({
    name: "yahoo-news",
    description: "Yahoo news headlines",
    takes: {search: noun_arb_text},
    icon: "http://headlines.yahoo.co.jp/favicon.ico",
	homepage: "http://code.google.com/p/ubiquity-commands-ja-nora/",
	author: {name: "hokunarin", email: "hokunarin@gmail.com"},
	license: "MPL",

    preview: function( previewBlock, directObject ) {

        var searchText = jQuery.trim(directObject.text);
        if(searchText.length < 1) {
            var previewStr = "<small>Yahoo! News を検索します</small>";
            previewBlock.innerHTML = previewStr;
            return;
        }

        var previewTemplate = "<b>${query}</b> を Yahoo! news で検索します...";
        var previewData = {query: searchText};
        previewBlock.innerHTML = CmdUtils.renderTemplate(previewTemplate, previewData);
        
        CmdUtils.previewAjax(previewBlock, {
          type: "GET",
          url: getYahooNewsUrl(searchText),
          dataType: "html",
          
          error: function() {
            previewBlock.innerHTML = "Error searching Yahoo! news.";
          },
          
          success: function(responseData) {
            
            responseData = jQuery(responseData);
            var items = [];
            var i = 0;
            
            responseData.find(".searchResult .article + .clearFix").slice(0,5).each(function(){
                
                var resultList = jQuery(this);
                
                var title = resultList.find(".yjMt").html();
                var url = resultList.find(".yjMt").attr("href");
                var description = resultList.find(".yjSt").html();
                var date = resultList.find(".yjSt ~ .date").text();
                var typeImg = "";
                resultList.find(".sqbtSmall").each(function(){
                    typeImg += jQuery(this).outerHTML();
                });
                var photo = resultList.find(".articlePhoto").html();
                
                var key = ++i;
                
                var item = {key : key,
                            title : title,
                            url : url,
                            description : description,
                            date : date,
                            typeImg : typeImg,
                            photo : photo
                            };
                
                items.push(item);
                
            });
            
            var previewData = {
              query : searchText,
              items : items
            };
            
            previewBlock.innerHTML = CmdUtils.renderTemplate(
              jQuery("#yahoo-news", feed.dom).html(),
              previewData);
          }
        });

    },

    execute: function( directObject ) {
        Utils.openUrlInBrowser( getYahooNewsUrl(directObject.text) );
    }
});

function getYahooNewsUrl(query){
    var url = "http://nsearch.yahoo.co.jp/bin/search?p={searchTerms}";
    url = url.replace(/%s|{searchTerms}/g, encodeURI(query) );
    return url;
}


jQuery.fn.outerHTML = function(s) {
    return (s) ? this.before(s).remove() : jQuery("<p>").append(this.eq(0).clone()).html();
}

]]></script>
</body>
</html>
